package cn.initcap.algorithm.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * lambda根据名称去除重复的并获取重复当中最大值
 *
 * @author initcap
 * @date Created in 12/3/18 3:10 PM.
 */
public class LambdaDistictNameFindMaxAge {

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>(5);
        personList.add(new Person("initcap", 1));
        personList.add(new Person("initcap", 5));
        personList.add(new Person("initcap", 2));
        personList.add(new Person("tcl", 3));
        personList.add(new Person("tcl", 1));
        Map<String, Integer> a = personList.stream().collect(Collectors.groupingBy(Person::getName, Collectors.reducing(0, Person::getAge, Integer::max)));
        List<Person> personList1 = personList.stream().filter(e -> a.containsKey(e.getName()) && a.containsValue(e.getAge())).collect(Collectors.toList());
        personList1.forEach(System.out::println);

    }

    static class Person {
        private String name;
        private Integer age;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}
